import time
import sys
import json
import base64
import pymysql
import logging
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service
# from selenium.webdriver.chrome.service import Service as ChromeService
# from webdriver_manager.chrome import ChromeDriverManager
'''
功能：根据演出名称爬取梅兰芳大剧院官网演出信息并写入数据表
入参：
    1、演出项目ID（数据表演出记录ID）
    2、演出项目名称(Base64转码)
    3、数据服务host
    4、数据服务端口
    5、数据库名称
    6、数据库用户名
    7、数据库密码(Base64转码)
    8、代理IP
    9、代理端口
'''
url = 'http://www.bjmlfdjy.cn/'
# 创建日志对象(不设置时默认名称为root)
log = logging.getLogger('md_perform_info_spider')
# 设置日志级别(默认为WARNING)
log.setLevel('INFO')
# 设置输出渠道(以文件方式输出需设置文件路径)
file_handler = logging.FileHandler('/data/out/md_perform_info_spider.log', encoding='utf-8')
file_handler.setLevel('INFO')
# 设置输出格式(实例化渠道)
fmt_str = '%(asctime)s %(thread)d %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s'
formatter = logging.Formatter(fmt_str)
# 绑定渠道的输出格式
file_handler.setFormatter(formatter)
# 绑定渠道到日志收集器
log.addHandler(file_handler)

if len(sys.argv) != 10:
    log.error('参数个数不匹配，请检测传入参数是否缺失！')
else:
    project_id = sys.argv[1]
    # base64解码
    project_name_byte = base64.decodebytes(sys.argv[2].encode('utf-8'))
    project_name = project_name_byte.decode('utf-8')
    db_host = sys.argv[3]
    db_port = sys.argv[4]
    db_name = sys.argv[5]
    db_user = sys.argv[6]
    # base64解码
    db_pwd_byte = base64.decodebytes(sys.argv[7].encode('utf-8'))
    db_pwd = db_pwd_byte.decode('utf-8')
    proxy_host = sys.argv[8]
    proxy_port = sys.argv[9]

    '''
    打开浏览器
    '''
    def __get_driver__(proxy_host, proxy_port):
        options = webdriver.FirefoxOptions()
        options.add_argument("--headless")
        options.add_argument('--no-sandbox')
        options.add_argument('--disable-gpu')
        options.add_argument('--disable-dev-shm-usage')
        options.add_argument(f"--proxy-server=http://{proxy_host}:{proxy_port}")
        service = Service(executable_path='/usr/local/bin/geckodriver')
        # windows环境
        # driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()), options=options)
        try:
            return webdriver.Firefox(options=options, service=service)
        except:
            log.error('打开浏览器失败！')
            pass
    '''
    访问网站并获取演出信息
    '''
    def __get_perform_info__(driver):
        driver.get(url=url)
        driver.implicitly_wait(1)
        driver.find_element(by=By.ID, value='J_keywor').send_keys(project_name)
        driver.find_element(by=By.CLASS_NAME, value='btn-serach').click()
        time.sleep(3)
        result_list = driver.find_element(by=By.ID, value='listcontent').find_element(by=By.TAG_NAME, value='ul').find_elements(by=By.TAG_NAME, value='li')
        if len(result_list) <= 0:
            log.error('未查询到演出信息！')
        else:
            result_list[0].find_element(by=By.CLASS_NAME, value='f_l').find_element(by=By.TAG_NAME, value='a').click()
            time.sleep(3)
            cover = driver.find_element(by=By.CLASS_NAME, value='item_c').find_element(by=By.CLASS_NAME, value='con_bg').find_element(by=By.CLASS_NAME, value='con_pic').get_attribute('src')
            description = driver.find_element(by=By.CLASS_NAME, value='item_nav').find_element(by=By.CLASS_NAME, value='inav_3').get_attribute('outerHTML')
            data = {
                'cover': cover,
                'description': description
            }
            log.info(json.dumps(data, ensure_ascii=False))
            return data
    '''
    数据入库
    '''
    def __insert_data_db_(data, project_id, project_name):
        db = pymysql.connect(
            host=db_host,
            port=int(db_port),
            db=db_name,
            user=db_user,
            password=db_pwd
        )
        cursor = db.cursor()
        sql = "UPDATE t_jymd_project t SET t.PROJECT_COVER='" + data['cover'] + "',t.PROJECT_DESC='" +  data['description'] + "' WHERE t.ID=" + project_id + " AND t.PROJECT_NAME='" + project_name + "'"
        effect_num = cursor.execute(sql)
        if effect_num <=0:
            log.error('爬取数据写入数据表失败！')
        db.commit()
        db.close()

    log.info('开始爬取演出信息！')
    # 打开浏览器
    log.info('1、打开浏览器')
    driver = __get_driver__(proxy_host, proxy_port)
    # 访问网站并获取演出信息
    log.info('2、爬取网站数据')
    data = __get_perform_info__(driver)
    # 数据入库
    if data is not None:
        log.info('3、数据入库')
        __insert_data_db_(data, project_id, project_name)
    log.info('爬取演出信息结束！')
    driver.close()
    driver.quit()
